#include "map.h"
#include <QPainter>
#include <QDebug>

//绘制地图

Map::Map(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(700,700);
    changeSize(16,16);
}

void Map::changeSize(const int &r, const int& c)
{
    //qDebug() << "nb" ;
    column=c;
    row=r;
    data=QVector<QVector<Flag>>(r,QVector<Flag>(c,null));
    start={-1,-1};
    end={-1,-1};
    update();
}
void Map::changeRoad(const int& i , const int& j , int & k )
{

    //qDebug() << "but";
    if(start!=QPoint{-1,-1})
    {
        data[start.x()][start.y()]=null;
    }
    start = {(start.x()+i),(start.y()+j)};
    if(data[start.x()][start.y()]  == roadBlock)
    {
        k = 1;
        QMessageBox::StandardButton mes1 = QMessageBox::information(NULL,"Error","You hava hit the stone!\nDo you want to start again?",QMessageBox::Yes|QMessageBox::No);
        switch (mes1)
        {
        case QMessageBox::Yes:
            changeSize(16,16);
            break;
        case QMessageBox::No:
            emit closeWid();
            break;
        default:
            break;
        }
    }
    else if(data[start.x()][start.y()]  == endPoint)
    {
        k = 1;
        QMessageBox::StandardButton mes1 = QMessageBox::information(NULL,"Congratulations","You hava finshed this task!\nDo you want to start again?",QMessageBox::Yes|QMessageBox::No);
        switch (mes1)
        {
        case QMessageBox::Yes:
            changeSize(16,16);
            break;
        case QMessageBox::No:
            emit closeWid();
            break;
        default:
            break;
        }
    }
    else if(start.x() < 0 || start.y() < 0 ||  start.x() > row || start.y() > column)
    {
        k = 1;
        QMessageBox::StandardButton mes1 = QMessageBox::information(NULL,"Error","You hava crossed the line!\nDo you want to start again?",QMessageBox::Yes|QMessageBox::No);
        switch (mes1)
        {
        case QMessageBox::Yes:
            changeSize(16,16);
            break;
        case QMessageBox::No:
            emit closeWid();
            break;
        default:
            break;
        }
    }
    else
    {
        k =0;
        data[start.x()][start.y()]=startingPoint;
        update();
    }

}

void Map::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    double xoffset=(width())/(double)column;
    double yoffset=(height())/(double)row;
    //画横线
    for(int i=0;i<row;i++)
    {
        painter.drawLine(0,i*yoffset,width(),i*yoffset);
    }
    painter.drawLine(0,height()-1,width(),height()-1);

    for(int i=0;i<column;i++)
    {
        painter.drawLine(i*xoffset,0,i*xoffset,height());
    }
    painter.drawLine(width()-1,0,width()-1,height());

    for(int i=0;i<data.size();i++)
    {
        for(int j=0;j<data[i].size();j++)
        {
            if(data[i][j]==startingPoint)
            {
                painter.fillRect(j*xoffset,i*yoffset,xoffset+1,yoffset+1,Qt::yellow);
            }
            else if(data[i][j]==endPoint)
            {
                painter.fillRect(j*xoffset,i*yoffset,xoffset+1,yoffset+1,Qt::green);
            }
            else if(data[i][j]==roadBlock)
            {
                painter.fillRect(j*xoffset,i*yoffset,xoffset+1,yoffset+1,Qt::black);
            }
        }
    }

}

void Map::mousePressEvent(QMouseEvent *event)
{
    double xoffset=(width())/(double)column;
    double yoffset=(height())/(double)row;
    int row=event->y()/yoffset;
    int col=event->x()/xoffset;
    if(event->button()==Qt::LeftButton)
    {
        if(start!=QPoint{-1,-1})
        {
            data[start.x()][start.y()]=null;
        }
        start={row,col};
        data[row][col]=startingPoint;
    }
    else if(event->button()==Qt::RightButton)
    {

//        if(end!=QPoint{-1,-1})
//        {
//            data[end.x()][end.y()]=null;
//        }
//        end={row,col};
        data[row][col]=endPoint;
    }
    else if(event->button()==Qt::MidButton)
    {
        data[row][col]=roadBlock;
    }
    update();
}


